package com.tianyang.yukuaifu.module.dialog;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.graphics.Bitmap;
import android.text.TextUtils;
import android.util.DisplayMetrics;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.target.SimpleTarget;
import com.bumptech.glide.request.transition.Transition;
import com.facebook.drawee.view.SimpleDraweeView;
import com.tianyang.yukuaifu.R;
import com.tianyang.yukuaifu.business.auth.bean.SampleImageOutput;
import com.tianyang.yukuaifu.utils.VerifyUtil;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Description：示例图片
 * Created by xyj on 2024/3/26
 */
public class SampleImageDialog extends Dialog {

    @BindView(R.id.pictureSdv)
    SimpleDraweeView pictureSdv;
    @BindView(R.id.describeTv)
    TextView describeTv;

    private Context mContext;

    public SampleImageDialog(Context context) {
        super(context, R.style.dialog_sample);
        setContentView(R.layout.dialog_sample_image);
        ButterKnife.bind(this);
        this.mContext = context;
        setCancelable(true);
    }

    public void showPictureDataView(SampleImageOutput.SampleImage output) {
        if (output != null) {
            describeTv.setText(VerifyUtil.checkString(output.getDescribe()));
            if (!TextUtils.isEmpty(output.getImageUrl())) {
                Glide.with(getContext())
                        .asBitmap()
                        .load(output.getImageUrl())
                        .into(new SimpleTarget<Bitmap>() {
                            @Override
                            public void onResourceReady(@NonNull Bitmap bitmap, @Nullable Transition<? super Bitmap> transition) {
                                DisplayMetrics metric = new DisplayMetrics();
                                ((Activity) mContext).getWindowManager().getDefaultDisplay().getMetrics(metric);
                                int pxWidth = metric.widthPixels;
                                int pxHeight = pxWidth * bitmap.getHeight() / bitmap.getWidth();
                                RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) pictureSdv.getLayoutParams();
                                params.width = pxWidth;
                                params.height = pxHeight;
                                pictureSdv.setLayoutParams(params);
                                pictureSdv.setImageBitmap(bitmap);
                            }
                        });
            }
        }
    }

    @OnClick({R.id.closeIv})
    public void onClick(View view) {
        if (view.getId() == R.id.closeIv) {
            dismiss();
        }
    }
}
